package cn.com.connected.common.aop;

import cn.com.connected.base.exception.BusinessException;
import cn.com.connected.base.exception.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import static cn.com.connected.common.emnus.HttpParams.UU1;

/**
 * Created by shaomingsheng on 2017/10/20.
 */
@Aspect
@Component
@Slf4j
@Order(1)
public class HeaderAOP {

  @Pointcut(value = "execution(* cn.com.connected.controller..*.*(..))")
  public void connectedPackage() {}

  @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
  public void requestMapping(){}

  @Pointcut("execution(* cn.com.connected.controller.open..*.*(..))")
  public void openPackage() {}



  @Order(1)
  @Around("requestMapping() && connectedPackage() && !openPackage()")
  public Object handlerLogin(ProceedingJoinPoint pjp)  throws Throwable{
    ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    HttpServletRequest request = attributes.getRequest();
    String uu1 = request.getHeader(UU1);
    if (uu1 == null){
      throw new BusinessException(ErrorCode.WARING,"非法请求");
    }

    Object result =  pjp.proceed();
    return result;
  }


}